package com.gzit.common.async;

import android.app.Activity;
import android.os.AsyncTask;

import com.gzit.common.async.LoadingCallback.DefaultLoadingCallback;

/**
 * 显示loading的异步任务 如果外界有自定义的loading方式，可以通过重写showLoading与hideLoading方法实现。
 * 
 * @author teddy
 * 
 * @param <Params>
 * @param <Progress>
 * @param <Result>
 */
public abstract class LoadingAsyncTask<Params, Progress, Result> extends
		AsyncTask<Params, Progress, Result> {

	protected Activity ctx;
	protected LoadingCallback loadingCallback;

	public LoadingAsyncTask(Activity ctx, LoadingCallback callback) {
		this.ctx = ctx;
		this.loadingCallback = callback == null ? new DefaultLoadingCallback(
				ctx) : callback;
	}

	public LoadingAsyncTask(Activity ctx) {

		this(ctx, new DefaultLoadingCallback(ctx));
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		if (this.loadingCallback != null && ctx != null && !ctx.isFinishing()) {
			this.loadingCallback.showLoading();
		}

	}

	protected void onPostExecute(Result result) {

		if (this.loadingCallback != null) {
			this.loadingCallback.hideLoading();
		}
	}

}
